package simple.rabbitmq.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@EnableBinding(Application.Topic.class)
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Slf4j
	@RestController
	static class Controller {
		@Autowired
		private Topic topic;

		@GetMapping("/message/{message}")
		public String message(@PathVariable(value = "message") String message) {
			log.info("Send: {}", message);
			topic.output().send(MessageBuilder.withPayload(message).setHeader("x-delay", 5000).build());
			return "ok";
		}
	}

	interface Topic {
		String OUTPUT = "output";

		@Output(OUTPUT)
		MessageChannel output();
	}
}
